package cn.xingyun.pickaxe_loli_new.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectUtil {
    public static Object getFieldValue(Object obj, String fieldName,String srgName) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(fieldName) || field.getName().equals(srgName)) {
                field.setAccessible(true);
                try {
                    return field.get(obj);
                }catch (Exception e) {
                    System.err.println("无法获取名为:"+fieldName+"的字段，其srg名为:"+srgName);
                }
            }
        }
        return null;
    }
    public static Object getFieldValue(Class clazz, String fieldName,String srgName) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(fieldName) || field.getName().equals(srgName)) {
                field.setAccessible(true);
                try {
                    return field.get(null);
                }catch (Exception e) {
                    System.err.println("无法获取名为:"+fieldName+"的字段，其srg名为:"+srgName);
                }
            }
        }
        return null;
    }
    public static void setFieldValue(Object obj, String fieldName,String srgName,Object value) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(fieldName) || field.getName().equals(srgName)) {
                field.setAccessible(true);
                try {
                    field.set(obj,value);
                }catch (Exception e) {
                    System.err.println("无法设置名为:"+fieldName+"的字段值，其srg名为:"+srgName);
                }
            }
        }
    }
    public static Object invokeMethod(Object obj, String methodName,String srgName, Object param[]) {
        Class<?> clazz = obj.getClass();
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName) || method.getName().equals(srgName)) {
                method.setAccessible(true);
                try {
                    return method.invoke(param);
                } catch (Exception e) {
                    System.err.println("无法执行方法:" + methodName);
                }
            }
        }
        return null;
    }
    public static Object invokeMethod(Class clazz, String methodName,String srgName, Object param[]) {
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName) || method.getName().equals(srgName)) {
                method.setAccessible(true);
                try {
                    return method.invoke(param);
                }catch (Exception e) {
                    System.err.println("无法执行方法:"+methodName);
                }
            }
        }
        return null;
    }
}
